package com.weng.platform.guest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.dao.mybatis.model.page.PageModel;
import com.weng.framework.dao.mybatis.service.BaseServiceImpl;
import com.weng.platform.guest.entity.TimeRecord;
import com.weng.platform.guest.mapper.TimeRecordMapper;
import com.weng.platform.guest.service.ITimeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;


/**
 * 生活记录 服务实现类
 *
 * @author weng
 * @date 2021-06-03 23:37:38
 *
 */ 
@Slf4j
@Service
public class TimeRecordServiceImpl extends BaseServiceImpl<TimeRecordMapper, TimeRecord> implements ITimeRecordService {
	@Autowired
	private TimeRecordMapper timeRecordMapper;
	

	/**
	 * 分页查询生活记录
	 * @return
	 */
	@Override
	public IPage<TimeRecord> list(Integer pageNo,Integer pageSize) {
		QueryWrapper<TimeRecord> qw = new QueryWrapper<>();
		IPage<TimeRecord> page = new PageModel<>(pageNo,pageSize);
		return timeRecordMapper.selectPage(page,qw);
	}


	/**
	 * 获取单个生活记录
	 * @param id
	 * @return
	 */
	@Override
	public TimeRecord getById(Serializable id) {
		return super.getById(id);
	}

	/**
	 * 新增单个生活记录
	 * @param timeRecord
	 * @return
	 */
	@Override
	public TimeRecord add(TimeRecord timeRecord)  {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		timeRecord.setDayStr(sf.format(new Date()));
		timeRecordMapper.insert(timeRecord);
		return timeRecord;
	}

	/**
	 * 更新单个生活记录
	 * @param timeRecord
	 * @return
	 * @
	 */
	@Override
	public int update(TimeRecord timeRecord)  {
		
		return timeRecordMapper.updateById(timeRecord);

	}


	@Override
	public ResponseData removeByIds(String ids) {
		String[] timeRecordIds = ids.split(",");
		boolean flag = this.removeByIds(Arrays.asList(timeRecordIds));
		if (flag) {
			return ResponseData.success();
		} else {
			return ResponseData.success();
		}
	}

}
